<template>
 <div v-show="!loading">
    <drawRightsTemp
    class="draw-trial-result"
      v-if="result"
      :equityGroups="trialRightPackage"
      btnText="畅享优惠 购买会员"
      @btnClick="onKnow"
      @wndClose="onClose">
      <div slot="important2">
        恭喜您获得7天免费{{ trialRightPackage | importantPackageName }}试用！
      </div>
    </drawRightsTemp>

    <drawRightsTemp
      v-else
      :equityGroups="trialRightPackage"
      btnText="畅享优惠 购买会员"
      @btnClick="onKnow"
      @wndClose="onClose">
      <div slot="important2">
        {{ trialRightPackage | importantPackageName(true) }}权益试用已经领完了哦~
      </div>
    </drawRightsTemp>
 </div>
</template>
<script>
import { getPackagesList } from '@/api/rights'
import { mapGetters } from 'vuex'
import drawRightsTemp from '@/components/member/draw-rights-temp.vue'
import { getCidByPkg } from '@/utils/costum'
export default {
  components: {
    drawRightsTemp
  },
  data () {
    return {
      result: false,
      loading: false,
      trialRightPackage: []
    }
  },
  computed: {
    ...mapGetters(['appInfo'])
  },
  created () {
    this.statIds = []
    this.initData()
  },
  methods: {
    pageLoadFinish () {
      if (['0', '1'].includes(this.$route.query.guide)) {
        this.$xz('xz_set_window_size', 'XZDesktopTrySuccess', 730 + 30, 560 + 30) // 同引导页一样
      } else {
        const h = Math.max(350, this.pkgH + 270)
        this.$xz('xz_set_window_size', 'XZDesktopTrySuccess', 640 + 30, h + 30) // ui窗口大小，数据超出会出现滚动条
      }
      this.$xz('xz_page_load_finished', 'XZDesktopTrySuccess')
    },
    initData () {
      this.loading = true
      getPackagesList({ pid: this.appInfo.allPackages }).then(res => {
        const list = res.data.list
        if (this.$route.query.c_pkgids) {
          this.filterRightPackage(list, this.$route.query.c_pkgids)
        } else {
          this.$store.dispatch('user/drawTrial').then(res => {
            res && this.$xz('xz_update_pay_result', true)
            this.filterRightPackage(list, res)
          }).catch(() => {
            this.loading = false
          })
        }
      }).catch(() => {
        this.loading = false
      })
    },
    filterRightPackage (list = [], c_pkgids = '') {
      const pkgids = c_pkgids.split(',')
      const triaLlist = list.filter(({ pkg_id }) => {
        return pkgids.includes(pkg_id)
      })
      this.result = triaLlist.length > 0
      if (this.result) {
        this.trialRightPackage = triaLlist
        this.$xzAsy('xz_get_stat_id').then(res => {
          if (!/^8\d{3}$/.test(res)) return
          const obj = {
            '10000': '9003',
            '20000': '9005',
            '40000': '9101'
          }
          for (const item of pkgids) {
            const actionId = res + obj[item]
            this.$xz('xz_substat_action', parseInt(actionId))
          }
        })
      } else {
        this.trialRightPackage = list
      }
      this.getPageHeight()
      this.loading = false
      this.statIds = this.setStatIds()
      this.statIds[0] && this.$xz('xz_substat_action', this.statIds[0])
    },
    getPageHeight () {
      let num = 0
      for (const item of this.trialRightPackage) {
        for (const itemb of item.cat_list) {
          const len = Math.min(itemb.right_list.length, 6)
          if (len > num) {
            num = len
          }
        }
      }
      this.pkgH = num * 22 + (num - 1) * 12
      this.pageLoadFinish()
    },
    setStatIds () {
      // [0显示,1我知道了,2关闭]
      const isNewInstall = this.$route.query.from === 'newinstallguide'
      const isBefore = this.$route.query.guide === '0'
      if (!this.$route.query.guide) return []
      if (this.result) {
        // 领取成功&&新手&&引导前 : 领取成功&&新手&&引导后
        if (isNewInstall) return isBefore ? [2532, 2533, 2534] : [2544, 2545, 2546]

        // 领取成功&&覆盖&&引导前 : 领取成功&&覆盖&&引导后
        return isBefore ? [2592, 2593, 2594] : [2604, 2605, 2606]
      }

      // 领完&&新手&&引导前 : 领完&&新手&&引导后
      if (isNewInstall) return isBefore ? [2535, 2536, 2537] : []

      // 领完&&覆盖&&引导前 : 领完&&覆盖&&引导后
      return isBefore ? [2595, 2596, 2597] : []
    },
    onKnow (packageItem) {
      if (this.appInfo.appID === '22') {
        const cid = getCidByPkg(packageItem.pkg_id)
        this.statIds[1] && this.$xz('xz_substat_action', this.statIds[1])
        this.$xz('xz_switch_dialog', 'XZDesktopOrderVIP', true, JSON.stringify({ cid, pkgid: packageItem.pkg_id }))
      }
      this.$xz('xz_switch_dialog', 'XZDesktopTrySuccess', false)
    },
    onClose () {
      this.statIds[2] && this.$xz('xz_substat_action', this.statIds[2])
      this.$xz('xz_switch_dialog', 'XZDesktopTrySuccess', false)
    }
  }
}
</script>
<style lang="scss">
.draw-rights-temp.draw-trial-result .draw-rights-content{
  padding: 0 25px;
}
</style>
